// pages/patient/condition/condition.js
var api = require('../../../config/api.js');
var utils = require('../../../utils/util.js');
var check = require('../../../utils/check.js');

const app = getApp();

var submitting = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    measureTime: "中",
		lienLength: '',
    leftArmline: '',
    rightArmline: '',
		lienState: "无"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.myName);
    this.setData({
      username: app.globalData.myName
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  putCondition: function(){
    let that = this;
    console.log("condition: " + JSON.stringify(that.data));
    if(submitting){
      console.log("on submitting ...");
      return;
    }
    if(check.isEmpty(that.data.username)
        ||check.isEmpty(that.data.measureTime)
        ||check.isEmpty(that.data.lienLength)
        ||check.isEmpty(that.data.lienState)
        ||check.isEmpty(that.data.rightArmline)
        ||check.isEmpty(that.data.leftArmline)){
      wx.showModal({
        title: '数据填写不完整'
      });
      return;
    }
    if(!check.isMaxLengthForDouble(that.data.leftArmline, 2, 1)){
      wx.showModal({
        title: '左侧臂围输入不合法'
      });
      that.setData({
        leftArmline: ''
      });
      return;
    }

    if(!check.isMaxLengthForDouble(that.data.rightArmline, 2, 1)){
      wx.showModal({
        title: '右侧臂围输入不合法'
      });
      that.setData({
        rightArmline: ''
      });
      return;
    }
    submitting = true;
    utils.request(api.putMeasure,{
      patientId: app.globalData.metaId,
      nurseId: app.globalData.myNurseId,
      measureTime: that.data.measureTime,
      lienLength: that.data.lienLength,
      exposure: that.data.exposure,
      leftArmline: that.data.leftArmline,
      rightArmline: that.data.rightArmline,
      lienState: that.data.lienState
    }, 'POST').then(function(res){
      submitting = false;
      console.log(JSON.stringify(res));
      if (res.errno == 0) {
        wx.showModal({
          title: '资料已提交成功',
          content: res.errmsg,
          showCancel: false,
          success: function(res){
            console.log("");
            if (res.confirm) {
              console.log('用户点击确定')
              wx.navigateBack({
                delta: 1
              });
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        });
      } else {
        wx.showModal({
          title: '错误信息',
          content: res.errmsg,
          showCancel: false
        });
      }
    }).catch(function(res){
      console.log("submit fail ...");
      submitting = false;
    });
  },

  radioChange: function(res){
    let that = this;
    console.log("选中的标签：" + res.detail.value);
    that.setData({
      measureTime: res.detail.value
    });
  },
// 接收用户输入的姓名
  userNameChanged: function(event){
    let that = this;
    console.log("姓名：" + event.detail);
    that.setData({
      username: event.detail
    });
  },
  // 接收用户输入的置留长度
  lienLengthChanged: function(event){
    let that = this;
    console.log("置留长度：" + event.detail);
    that.setData({
      lienLength: event.detail
    });
  },
  // 接收用户输入的外露长度
  exposureChanged: function(event){
    let that = this;
    console.log("外露长度：" + event.detail);
    that.setData({
      exposure: event.detail
    });
  },
  // 接收用户输入的左侧臂围
  leftArmlineChanged: function(event){
    let that = this;
    console.log("左侧臂围：" + event.detail);
    that.setData({
      leftArmline: event.detail
    });
  },
  // 接收用户输入的右侧臂围
  rightArmlineChanged: function(event){
    let that = this;
    console.log("右侧臂围：" + event.detail);
    that.setData({
      rightArmline: event.detail
    });
  },
  // 接收用户输入的穿刺点状态
  lienStateChanged: function(event){
    let that = this;
    console.log("穿刺点状态：" + event.detail);
    that.setData({
      lienState: event.detail
    });
  },
})